import 'package:flutter/material.dart';
import 'package:flutter_app_05/action/states/dark_states.dart';
import 'package:flutter_app_05/action/states/data_manager.dart';
import 'package:flutter_app_05/page/setup_page.dart';
import 'package:flutter_app_05/utils/MyTheme.dart';
import 'package:flutter_app_05/utils/ScreenAdapter.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:provider/provider.dart';
import 'package:flutter_app_05/widget/stateful_widget_extends.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import 'login/login.dart';
import 'package:flutter_app_05/widget/sdk_extends.dart';

class MinePage extends StatefulWidget {
  @override
  MinePageState createState() => new MinePageState();
}

enum ConferenceItem { AddMember, LockConference, ModifyLayout, TurnoffAll }

class MinePageState extends State<MinePage> {
  static String flag;
  static String flag2;
  int fl=0;
  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
    return Consumer<DartState>(builder: (context, categoryState, child) {
        categoryState.initMode();
        print("现在的状态" + categoryState.darkMode.toString());
        return columns(categoryState);
      });
  }

  Widget columns(categoryState) {
    categoryState.initMode();
    Widget devide = SizedBox(
      height: 30,
    );
    return Container(
      color:categoryState.darkMode == 1
          ? MyTheme.colorLight['bg']
          : MyTheme.colorDark['dark_bg'],
      child: Column(
        children: [
          Container(
            alignment: Alignment.center,
            height: 250,
            color: categoryState.darkMode == 1
                ? MyTheme.colorLight['app_green']
                : MyTheme.colorDark['dark_app_green'],
            child: Stack(
              children: [
                Positioned(
                  child:
                  "https://avatar-static.segmentfault.com/229/294/229294182-5d46e5a4cf510_huge256"
                      .getImage(
                    circle: true,
                    width: 80,
                    height: 80,
                  ),
                  left: 15,
                  bottom: 10,
                ),
                Positioned(
                  child: Text(
                    '你猜猜我是谁',
                    style: TextStyle(
                      fontSize: 20.0, // 文字大小
                      color: Colors.white, // 文字颜色
                    ),
                  ),
                  left: 105,
                  bottom: 50,
                ),
                Positioned(
                  child: Text(
                    '有点懒',
                    style: TextStyle(
                      fontSize: 15.0, // 文字大小
                      color: Colors.white, // 文字颜色
                    ),
                  ),
                  left: 105,
                  bottom: 20,
                ),
                Positioned(
                  child: InkWell(
                    child: new Image(
                      image: new AssetImage("images/icon_qt.png"),
                      width: 25,
                      height: 25,
                    ),
                  ),
                  right: 10,
                  bottom: 40,
                ),
                Positioned(
                  child: PopupMenuButton(
                    child: new Image(
                      image: new AssetImage("images/icon_sz.png"),
                      width: 30,
                      height: 30,
                    ),
                    itemBuilder: (BuildContext context) {
                      flag =
                      categoryState.darkMode == 1 ? "切换到黑夜模式" : "切换到白天模式";
                      flag2 = fl==1?"退出":"登录";
                      setState(() {

                      });
                      return [
                        PopupMenuItem(
                          child: Text(flag2),
                          value: 'a',
                        ),
                        PopupMenuItem(
                          child:
                          Text(categoryState.darkMode == 1 ? flag : flag),
                          value: 'f',
                        ),
                      ];
                    },
                    onSelected: (String result) {
                      switch (result) {
                        case 'a':
                          if(fl==0) {
                            this.push(LoginPage()).then((value) {
                              ifLogin();
                              setState(() {

                              });
                            }) ;
                          }else{
                            LoginOut();
                            this.push(LoginPage()).then((value) {
                             ifLogin();
                              setState(() {

                              });
                            }) ;
                          }
                          setState(() {

                          });
                          break;
                        case 'f':
                          if (categoryState.darkMode == 1) {
                            flag = "切换黑夜模式";
                            Provider.of<DartState>(context, listen: false)
                                .changeMode(2);
                          } else {
                            Provider.of<DartState>(context, listen: false)
                                .changeMode(1);
                            flag = "切换到白天模式";
                          }
                          break;
                      }
                    },
                    onCanceled: () {
                      print("canceled");
                    },
                  ),
                  right: 10,
                  bottom: 150,
                ),
              ],
            ),
          ),
          devide,
          Container(
              margin: EdgeInsets.symmetric(
                  horizontal: ScreenAdapter.width(15),
                  vertical: ScreenAdapter.width(15)),
              width: 460,
              height: 60,
              child: RaisedButton(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(15)),
                ),
                color:
                categoryState.darkMode == 1 ? Colors.white : MyTheme.colorDark['dark_app_green'],
                child: Row(
                  children: <Widget>[
                    MyText(categoryState,"供热联盟"),
                  ],
                ),
                padding: EdgeInsets.fromLTRB(30, 0, 0, 0),
                onPressed: () {
                  print(1);
                  const url = "https://www.cngrlm.com";
                  launch(url);
                },
              )),
          Container(
              margin: EdgeInsets.only(
                  left: ScreenAdapter.width(15),
                  right: ScreenAdapter.width(15)),
              width: 460,
              height: 60,
              decoration: BoxDecoration(
                  border: Border(
                      bottom: BorderSide(
                        width: 0.5,
                        color: Colors.black12,
                      ))),
              child: RaisedButton(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(15.0),
                    topRight: Radius.circular(15.0),
                  ),
                ),
                color:
                categoryState.darkMode == 1 ? Colors.white : MyTheme.colorDark['dark_app_green'],
                child: Row(
                  children: <Widget>[
                    MyText(categoryState,"配置"),
                  ],
                ),
                padding: EdgeInsets.fromLTRB(30, 0, 0, 0),
                onPressed: () {
                  this.push(SetUpPage());
                },
              )),
          Container(
              margin: EdgeInsets.only(
                  left: ScreenAdapter.width(15),
                  right: ScreenAdapter.width(15)),
              width: 460,
              height: 60,
              child: RaisedButton(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(15.0),
                    bottomRight: Radius.circular(15.0),
                  ),
                ),
                color:
                categoryState.darkMode == 1 ? Colors.white : MyTheme.colorDark['dark_app_green'],
                child: Row(
                  children: <Widget>[
                    MyText(categoryState,"设置"),

                  ],
                ),
                padding: EdgeInsets.fromLTRB(30, 0, 0, 0),
                onPressed: () {},
              )),
          Container(
              margin: EdgeInsets.symmetric(
                  horizontal: ScreenAdapter.width(15),
                  vertical: ScreenAdapter.width(15)),
              width: 460,
              height: 60,
              child: RaisedButton(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(15)),
                ),
                color:
                categoryState.darkMode == 1 ? Colors.white : MyTheme.colorDark['dark_app_green'],
                child: Row(
                  children: <Widget>[
                    MyText(categoryState,"关于我们"),
                  ],
                ),
                padding: EdgeInsets.fromLTRB(30, 0, 0, 0),
                onPressed: () {
                  print(2);
                  const url =
                      "https://www.lcarbo.cn/portal.php?mod=list&catid=2";
                  launch(url);
                },
              )),
        ],
      ),
    );
  }

  Future<bool> ifLogin() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    int a=prefs.get("iflogin")??0;
    print(a.toString()+"miaojinlong");
    if(a==0){
      fl=0;
    }else{
      fl=1;
    }
  }

  Future<bool> LoginOut() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setInt("iflogin",0);
    setState(() {

    });

  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _initPersonInfo();
    if(ifLogin()==true){
      fl=1;
    }else{
      fl=0;
    }
    setState(() {

    });
  }

  Future<bool> _initPersonInfo() async {
    var a=DataManager.getGetlcb(p: {"page":"1","perPage":"1"});
  }

  Widget MyText(categoryState,String msg){
    return Text("${msg}",style: TextStyle(color: categoryState.darkMode == 1 ? MyTheme.colorLight['colorBlack']: MyTheme.colorDark['colorBlack']),);
  }
}
